using KL.Standard.Core.Utils;
using KL.Standard.Domain.Repositories;
using KL.Standard.Domain.Services;
using KL.Standard.Model.Contant;
using KL.Standard.Model.Infrastructure;
using KL.Standard.Model.Models.BaseArch;
using KL.Standard.Model.ViewModels.InParams;
using KL.Standard.Model.ViewModels.OutResult;

namespace KL.Standard.Services;

public class UserSvc :  IUserSvc
{

    public IUserRepository UserRepository { set; get; }

    public IMenuRepository MenuRepository { set; get; }

    public IMenuFunctionRepository MenuFunctionRepository { set; get; }

    #region 登录
    public BaseResult<CurUserInfo> Login(LoginParam p)
    {
        var userInfo = UserRepository.QueryUserInfo(p.loginID);
        if (userInfo == null)
            return new BaseResult<CurUserInfo>()
            {
                success = false,
                error = "账号或者密码错误"
            };
        
        if (!string.IsNullOrEmpty(userInfo.password)
            && userInfo.password.Equals(MD5Util.Get32MD5One(p.loginPwd)))
        {
            if (userInfo.isLock == 1)
            {
                return new BaseResult<CurUserInfo>()
                {
                    success = false,
                    error = "账号已经被禁用"
                };
            }
            
            //userInfo.loginLastIP = new IPUtility().GetIP();
            UserRepository.UpdateLoginInfo(userInfo);
            return new BaseResult<CurUserInfo>()
            {
                success = true,
                error = "",
                data = new CurUserInfo(userInfo)
            };
            
        }
        
        return new BaseResult<CurUserInfo>()
        {
            success = false,
            error = "账号或者密码错误"
        };
        
        
        //return null;
    }



    #endregion


    public async Task<PageResult<AccountVM>> SearchPage(PageSearchModel searchInfo)
    {
        return await UserRepository.SearchPage(searchInfo);
    }

    public BaseResult<string> Save(SysAccountPO info)
    {

        if (UserRepository.IsExist(info))
        {
            return new BaseResult<string>()
            {
                success = false,
                error = "系统已经存在相同账号",
            };
        }
        else
        {
            long count = UserRepository.SaveInfo(info);
            if (count > 0)
                return new BaseResult<string>()
                {
                    success = true,
                    error = "保存成功！"
                };
            else
            {
                return new BaseResult<string>()
                {
                    success = false,
                    error = "保存失败！"
                };
            }
        }
    }

    public BaseResult<string> ChangePwd(CurUserInfo curUserInfo, string newPwd, string oldPwd)
    {
        var userInfo = UserRepository.QueryUserInfo(curUserInfo.loginID);
        if (userInfo == null)
            return new BaseResult<string>()
            {
                success = false,
                error = "用戶信息有误，请重新登录！"
            };
        else
        {
            if (!string.IsNullOrEmpty(userInfo.password)
                && userInfo.password.Equals(MD5Util.md5(oldPwd, 32)))
            {
                int count = UserRepository.UpdateNewPwd(curUserInfo.loginID, MD5Util.md5(newPwd, 32));
                if (count > 0)
                {
                    return new BaseResult<string>()
                    {
                        success = true,
                        error = "修改成功！"
                    };
                }
                else
                {
                    return new BaseResult<string>()
                    {
                        success = false,
                        error = "修改失败！"
                    };
                }
            }
            else
            {
                return new BaseResult<string>()
                {
                    success = false,
                    error = "旧密码错误！"
                };
            }
        }
    }

    // public bool ExecuteSynCommand(SysAccountPO msg, string opeType)
    // {
    //     if (opeType.Equals(SYN_OPTTYPE.SAVE))
    //     {
    //         var count = UserRepository.SaveInfo4ZT(msg);
    //         return count > 0;
    //     }
    //
    //     if (opeType.Equals(SYN_OPTTYPE.DELETE))
    //     {
    //         var count = UserRepository.Delete(new string[]{msg.loginID});
    //         return count > 0;
    //     }
    //     if (opeType.Equals(SYN_OPTTYPE.RESTPWD))
    //     {
    //         var count = UserRepository.RestPwd(new string[]{msg.loginID});
    //         return count > 0;
    //     }
    //     if (opeType.Equals(SYN_OPTTYPE.LOCKORUN))
    //     {
    //         var count = UserRepository.Lock(msg.loginID);
    //         return count > 0;
    //     }
    //     return false;
    // }

    public BaseResult<string> Save4ZT(SysAccountPO sysAccountPo)
    {
        var count = UserRepository.SaveInfo4ZT(sysAccountPo);
        return new BaseResult<string>()
        {
            success = count>0,
            error = count>0?"操作成功":"操作失败"
        };
    }

    public BaseResult<string> Delete4ZT(string loginId)
    {
        int count = UserRepository.Delete(new []{loginId});
        return new BaseResult<string>()
        {
            success = count > 0,
            error = count > 0 ? "操作成功！" : "操作失败！"
        };
    }

    public async Task<BaseResult<IEnumerable<TreeNode<string>>>> GetAllUnChkMenus4Account(string loginID)
    {
        var nodes = await MenuRepository.GetUnChkMenus4Tree(loginID);
        var funs = await MenuFunctionRepository.GetUnChkMenuFunctions4Tree(loginID);
        var parentNodes = nodes.Where(i => i.pID == "M0").ToList();
        var NOparentNodes = nodes.Where(i => i.pID != "M0").ToList();
        NOparentNodes.ForEach(i =>
        {
            i.children = funs.Where(t => t.pID == i.id).OrderBy(j => j.id).ToList();
        });
        parentNodes.ForEach(i =>
        {
            i.children = NOparentNodes.Where(t => t.pID == i.id).ToList();
        });
        return new BaseResult<IEnumerable<TreeNode<string>>>()
        {
            success = true,
            error = "",
            data = parentNodes
        };
    }

    // public bool UpdataAccountAuthor(OutSysAccountAuthorVM info)
    // {
    //     int count = UserRepository.UpdataAccountAuthor(info);
    //     return count > 0;
    // }
}